package p.ithorns.sample.websocket.model;


import p.ithorns.framework.common.utils.StringUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * WsMsgType
 *
 * Websocket消息类型
 *
 * @author liuhaitao
 * @version 1.0.0
 * @date 2022/4/15 14:12
 */
public enum WsMsgType {

    /**
     * 空
     */
    NONE(""),

    /**
     * 心跳
     */
    HEART_BEAT("HB"),

    /**
     * 标签卡定位数据
     */
    CARD("WS_CARD"),

    /**
     * 信标低电量
     */
    BEACON("WS_BEACON"),

    ;

    private final static Map<String, WsMsgType> MAP = Arrays.stream(WsMsgType.values())
            .collect(Collectors.toMap(WsMsgType::getType, v -> v, (v1, v2) -> v2));

    private final static Map<String, Class<?>> TYPE_MAP = new HashMap<>();


    private final String type;

    WsMsgType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public static WsMsgType typeOf(String type) {
        if (StringUtil.isBlank(type)) {
            return NONE;
        }

        return Optional.ofNullable(MAP.get(type))
                .orElse(NONE);
    }

    public static void register(String type, Class<?> clazz) {
        TYPE_MAP.put(type, clazz);
    }

    public static Class<?> getClazz(String type) {
        return TYPE_MAP.get(type);
    }
}
